<?php


class AppConfigPlugin {

	
	/**
	 * Retorna el objeto Menu con sus propiedades seteadas
	 */
	public static function getMenuItems($rol = null) {
		require('conf/configuration.php');
		
		// si el rol es "administrador" retornar todos los menu items
		if ($rol == "0" || $rol == null) {
			return $menuItems;
		}
			
		// sino, filtrar solo los menu permitidos
		$accessMenuItems = array();
		foreach ($menuItems as $menuItem) {
			//echo "<br/>rol usuario: " . $rol;
			//echo "<br/>rol pagina: " . $menuItem[rol];
			if (strpos($rol, $menuItem[rol]) !== false)
				$accessMenuItems[] = $menuItem;
		}
		return $accessMenuItems;
		
	}
	
	
	/**
	 * Retorna el objeto Manage con sus propiedades seteadas
	 */
	public static function getManageItems() {
		require('conf/configuration.php');
		return $manageItems;
		
	}

	
	
	/**
	 * Retorna app info
	 */
	public static function getAppInfo() {
		require('conf/configuration.php');
		return $appInfo;
	}
	


	public static function search($array, $script, &$breadcrumb, &$found) {
		for($i = 0; $i <= count($array) && !$found; $i++) {
			$item = $array[$i];
			if ($item[script] == $script) {
				$breadcrumb[] = array("name" => $item[name], "script" => $item[script], "image" => $item[image]);
				$found = true;
			} else {
				if (($item[sons] != null) && (count($item[sons]) > 0)) {
					$breadcrumb[] = array("name" => $item[name], "script" => $item[script]);
					$found = AppConfigPlugin::search($item[sons], $script, $breadcrumb, $found);
					if (!$found)
						array_pop($breadcrumb);
				}
			}
		}
		return $found;
	}
	
	/**
	 * Retorna page tree
	 */
	public static function getBreadcrumb($script) {
		require('conf/configuration.php');
		$found = false;
		$breadcrumb = array(); 
		$exist = AppConfigPlugin::search($pageTree, $script, $breadcrumb, $found);
		return $breadcrumb;
	}
	
	
	
	public static function sons($array, $script, &$sons, &$found) {
		for($i = 0; $i <= count($array) && !$found; $i++) {
			$item = $array[$i];
			if ($item[script] == $script) {
				$sons = $item[sons];
				$found = true;
			} else {
				if (($item[sons] != null) && (count($item[sons]) > 0)) {
					//$breadcrumb[] = array("name" => $item[name], "script" => $item[script]);
					$found = AppConfigPlugin::sons($item[sons], $script, $sons, $found);
					/*if (!$found)
						array_pop($breadcrumb);*/
				}
			}
		}
		return $found;
	}
	

	/**
	 * Retorna page tree
	 */
	public static function getSons($script) {
		require('conf/configuration.php');
		$found = false;
		$sons = array(); 
		$exist = AppConfigPlugin::sons($pageTree, $script, $sons, $found);
		return $sons;
	}
	
	
	/**
	 * Retorna los iconos img
	 */
	public static function getIconsImg() {
		require('conf/configuration.php');
		return $iconsImg;
		
	}

	
	/**
	 * Retorna la cantidad de items para mostrar en cada pagina
	 */
	public static function getTotalItemsPerPage() {
		require('conf/configuration.php');
		return $totalItemsPerPage;
	}

	
	/**
	 * Retorna el nombre del script php actual (licencias.php)
	 */	
	public static function getCurrentScript() {
		$file = $_SERVER["SCRIPT_NAME"];
		$break = explode('/', $file);
		return $break[count($break) - 1];		
	}

	
	/**
	* Retorna la url con los parametros de GET incluido (ej. licencias.php?action=add)
	*/
	public static function curPageURL() {
		$fullUrl = parse_url($_SERVER['REQUEST_URI']);

		$url =(strrchr($fullUrl['path'],'/'));

		$action = $_GET['action'];
		if (!isset($action))
			$action = $_POST['action'];

		$seccional = $_GET['s'];
		if (!isset($seccional))
			$seccional = $_POST['s'];

		if(isset($seccional)) {
			if(!empty($action))
				//URL tiene action y Seccional
				$path= $url."?action=".$action."&s=".$seccional;
			else
				//URL tiene solo Seccional	
				$path= $url."?s=".$seccional;
		}
		else{ 
			if($action!="")
				//URL tiene solo Seccional
				$path= $url."?action=".$action;
			else 
				$path= $url;
		}
		return str_replace("/","",$path);
	}
	
}